package com.ethink.warehouse.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ethink.warehouse.api.info.WarehouseInfo;
import com.ethink.warehouse.common.entity.SubWarehouse;
import com.ethink.warehouse.common.entity.Warehouse;
import com.ethink.warehouse.common.entity.WarehouseMateMap;
import com.ethink.warehouse.common.mapper.SubWarehouseMapper;
import com.ethink.warehouse.common.mapper.WarehouseMapper;
import com.ethink.warehouse.common.mapper.WarehouseMateMapMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @Author: lzy
 * @Date: Created in 2022/11/22 15:48
 * ModifiedBy:
 * Version:
 */
@Slf4j
@Component
public class WarehouseMateMapManager {
    @Autowired
    private WarehouseMateMapMapper warehouseMateMapMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;

    /**
     * 获取外部仓储商品ID
     * @param subWarehouseCode
     * @param mateSku
     * @return
     */
    public String getMateItemId(String subWarehouseCode, String mateSku) {
        WarehouseInfo warehouseInfo = warehouseMapper.getWarehouseBySubWarehouseCode(subWarehouseCode);
        if (warehouseInfo == null) {
            return null;
        }
        LambdaQueryWrapper<WarehouseMateMap> warehouseMateMapQueryWrapper = new LambdaQueryWrapper<>();
        warehouseMateMapQueryWrapper.eq(WarehouseMateMap::getWarehouseCode, warehouseInfo.getWarehouseCode());
        warehouseMateMapQueryWrapper.eq(WarehouseMateMap::getMateSku, mateSku);
        warehouseMateMapQueryWrapper.eq(WarehouseMateMap::getIsDeleted, false);
        WarehouseMateMap warehouseMateMap = warehouseMateMapMapper.selectOne(warehouseMateMapQueryWrapper);
        if (warehouseMateMap == null) {
            return null;
        }

        return warehouseMateMap.getMateItemId();
    }

    public String getMateSku(String subWarehouseCode, String mateItemId) {
        WarehouseInfo warehouseInfo = warehouseMapper.getWarehouseBySubWarehouseCode(subWarehouseCode);
        if (warehouseInfo == null) {
            return null;
        }
        LambdaQueryWrapper<WarehouseMateMap> warehouseMateMapQueryWrapper = new LambdaQueryWrapper<>();
        warehouseMateMapQueryWrapper.eq(WarehouseMateMap::getWarehouseCode, warehouseInfo.getWarehouseCode());
        warehouseMateMapQueryWrapper.eq(WarehouseMateMap::getMateItemId, mateItemId);
        warehouseMateMapQueryWrapper.eq(WarehouseMateMap::getIsDeleted, false);
        WarehouseMateMap warehouseMateMap = warehouseMateMapMapper.selectOne(warehouseMateMapQueryWrapper);
        if (warehouseMateMap == null) {
            return null;
        }

        return warehouseMateMap.getMateSku();
    }
}
